package com.timothy.chart;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Paint.Style;
import android.util.Log;
import android.view.View;

public class PieView extends View{
	private final static String LOG_TAG = "PieView";
	
	int areaX = 4;
	int areaY = 100;
	
	int areaWidth;
	int areaHight;
	
	int colors[];
	int shade_colors[];
	int percent[];
	private int thickness=20;
	
	int width;
	int height;
	
	/**
	 * @param context 上下文
	 * @param colors 最上面颜色数组
	 * @param shade_colors 阴影颜色数组
	 * @param percent 百分比 (和必须是360)
	 */
	public PieView(Context context,int[] colors,int[] shade_colors,int[] percent) {
		super(context);
		this.colors = colors;
		this.shade_colors = shade_colors;
		this.percent = percent;
	}
	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

		height = View.MeasureSpec.getSize(heightMeasureSpec); 
		width = View.MeasureSpec.getSize(widthMeasureSpec); 
		setMeasuredDimension(width,height); 
		
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
	}
	
	public void setThickness(int thickness) {
		this.thickness = thickness;
		areaY=thickness+2;
		this.invalidate();
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		
		
		areaWidth = width-2;
		areaHight = height-2;
		
		Paint paint = new Paint();
		paint.setColor(Color.RED);
		paint.setStyle(Style.FILL);
		paint.setAntiAlias(true);
		paint.setStrokeWidth(1);
		
		paint.setTextSize(25);
		canvas.drawText("Are you happy?", 50, 30, paint); 
		
		//start draw pie
		for(int i=0;i<=thickness;i++){
			int tempAngle=0;
			for(int j=0;j<percent.length;j++){
				paint.setColor(shade_colors[j]);
				Log.i(LOG_TAG,"canvas.drawArc:"+"Color="+shade_colors[j]+" x="+areaX+" y="+(areaY-i)+" areaWidth="+(areaX+areaWidth)+
						" areaHight="+(areaHight-i) + " tempAngle="+tempAngle + " percent" +percent[j]);
				canvas.drawArc(new RectF(areaX, areaY-i, areaX+areaWidth, areaHight-i), tempAngle,percent[j], true, paint);
				tempAngle+=percent[j];
			}
			if(i==thickness){
				Log.i(LOG_TAG,"thickness="+thickness);
				for(int j=0;j<percent.length;j++){
					paint.setColor(colors[j]);
					Log.i(LOG_TAG,"canvas.drawArc:"+"Color="+shade_colors[j]+" x="+areaX+" y="+(areaY-i)+" areaWidth="+(areaX+areaWidth)+
							" areaHight="+(areaHight-i) + " tempAngle="+tempAngle + " percent" +percent[j]);
					canvas.drawArc(new RectF(areaX, areaY-i, areaX+areaWidth, areaHight-i), tempAngle,percent[j], true, paint);
					tempAngle+=percent[j];
				}
			}
		}
	}
}
